home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 3⁄23⁄90 / 0084-C⁄C++ BUG-Mar90 < prev    next >
Encoding:
Text File  |  1990-03-23  |  2.2 KB  |  64 lines  |  [TEXT/GEOL]

  1. Item    9466606                         20-March-90        09:47PST
  2.  
  3. From:   ROSENSTEIN1                     Rosenstein, Larry
  4.  
  5. To:     CPLUS.APPLE$                    C++ Interest List--Apple Employees
  6.         CPLUS.DEV$                      C++ Interest List--Developers
  7.         MACAPP.TECH$                    MacApp Technical
  8.         APPLE.BUGS                      Apple Bugs Reporting
  9.  
  10. Sub:    C/C++ BUG
  11.  
  12. Attn:   C++ Apple
  13. Attn:   C++ Public
  14. Attn:   MacApp Tech
  15. Attn: Apple Bugs
  16. SentBy: Larry Rosenstein
  17. Subject:  C/C++ BUG                                     3/19/90      9:55 AM
  18. There's a subtle C compiler bug that you might run into while using C++
  19. (especially if you are using it with MacApp).  The bug is demonstrated by the
  20. program at the end.
  21.  
  22. If you compile this and dump the object code for the TestBug routine, you will
  23. see that the compiler places the result of the GetRect call into a local
  24. variable, and then tries to copy that local into r.  Unfortunately, when it
  25. does the copy it uses the wrong local variable.
  26.  
  27. It is difficult to characterize the kind of C++ code that causes the bug.  It
  28. is necessary that you call a function with Pascal calling conventions that
  29. returns a value larger than 4 bytes.  It is also seems necessary to have a
  30. statement where CFront must create a temporary variable.  (Take a look at the
  31. generated C code to see what I mean.)
  32.  
  33. In the example below, the result of GetFoo is assigned to a CFront-generated
  34. temporary variable, before GetRect is called.  If you assigne the result to an
  35. explicit local variable, then the output from the C compiler is OK.  The same
  36. is true if you remove the word 'pascal' from the declaration of GetRect.
  37.  
  38. This is a bug in the C compiler, but the code that causes the problem is
  39. relatively obscure, and seems likely to appear only if you are using C++.
  40. Also, since it is necessary to use Pascal calling conventions, it is most
  41. likely to affect programmers trying to interface C++ and Pascal.
  42.  
  43. Larry Rosenstein
  44.  
  45.  
  46. #include <Types.h>
  47.  
  48. class TFoo: public PascalObject {
  49. public:
  50.     virtual pascal Rect GetRect();
  51. };
  52.  
  53. class TBar: public PascalObject {
  54. public:
  55.       virtual pascal TFoo* GetFoo();
  56. };
  57.  
  58. pascal void TestBug(TBar* aBar)
  59. {
  60.    Rect r = (aBar -> GetFoo()) -> GetRect();
  61. }
  62.  
  63.  
  64.